package com.shanks.home.net;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class SocketOutputStream {
	private boolean 				m_bBigEndian = true;
	private DataOutputStream		m_out = null;
	
	public SocketOutputStream(ByteArrayOutputStream out){
		m_out = new DataOutputStream(out);
		m_bBigEndian = true;
	}
	
	public SocketOutputStream(ByteArrayOutputStream out, boolean bBigEndian){
		m_out = new DataOutputStream(out);
		m_bBigEndian = bBigEndian;
	}	
	
	public void write(byte[] buffer) throws IOException {
		m_out.write(buffer);
	}
	
	public void writeByte(byte val) throws IOException{
		m_out.writeByte(val);
	}
		
	public void writeShort(short val) throws IOException{
		if (m_bBigEndian)
			m_out.writeShort(val);
		else
			m_out.writeShort(Short.reverseBytes(val));
	}
	
	public void writeInt(int val) throws IOException{
		if (m_bBigEndian)
			m_out.writeInt(val);
		else
			m_out.writeInt(Integer.reverseBytes(val));
	}
	
	public void writeLong(long val) throws IOException{
		if (m_bBigEndian)
			m_out.writeLong(val);
		else
			m_out.writeLong(Long.reverseBytes(val));
	}	
	
	public void close() throws IOException{
		m_out.close();
	}
	
}
